<template>
  <!-- 协催管理-诉讼案件-->
  <section class="page assist-case">
    <data-form :model="model" @onSearch="refreshData" @handleResetForm="handleResetForm">
      <template slot="inputs">
        <el-form-item label="借据号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="案件金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="逾期阶段" prop="overduePeriod">
          <el-select v-model="model.overduePeriod" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="model.overdueDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="系统来源" prop="sysCode">
          <el-select v-model="model.sysCode" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('SysCode')" :key="index" :label="name" :value="code.replace(/M|\+/, '')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="model.productName"></el-input>
        </el-form-item>
        <el-form-item label="诉讼编号" prop="lawNumber">
          <el-input v-model="model.lawNumber"></el-input>
        </el-form-item>
         <el-form-item label="诉讼状态" prop="lawerStatus">
          <el-select v-model="model.lawerStatus" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('LawerStatus').filter(m=>m.name==='撤诉'||m.name==='结案'||m.name==='执行终结'||m.name==='参与分配')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="顾客类型" prop="customerCategory">
          <el-select v-model="model.customerCategory" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('CustomerCategory')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="创建时间" prop="creatTime">
          <el-date-picker type="daterange" :editable="false" v-model="model.creatTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :page="pageService">
      <template slot="buttons">
        <el-button @click="distributeClick" v-auth = "1513">重新分配</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button v-if="scope.row.assistStatus !== 'ASSIST_OVER'" type="text" @click="isView=false,lawsuitClick(scope.row)">诉讼跟进</el-button>
            <el-button v-if="scope.row.assistStatus === 'ASSIST_OVER' && scope.row.lawerStatus && scope.row.lawsuitFollowType" type="text" @click="isView=true,lawsuitClick(scope.row)">跟进查看</el-button>
            <el-button type="text" @click="rowData=scope.row, dialog.isOpenCostMaintenance=true">费用维护</el-button>
          </template>
        </el-table-column>
        <el-table-column  label="借据号" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
          <template slot-scope="scope">
            <a  @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</a>
          </template>
        </el-table-column>
        <el-table-column prop="lawNumber" label="诉讼编号" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :formatter="(row)=>$filter.encryptPhone(row.mobileNo)" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :formatter="(row)=>$filter.encryptIDCardFour(row.idCard)" :min-width="$helper.getColumnWidth(6)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overdueAmount" label="案件金额(元)" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="overduePeriod" label="逾期阶段" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="sysCode" label="系统来源" :formatter="(row) => $filter.encryptSysCode(row.sysCode)" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="productName" label="产品名称" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column>
        <!-- <el-table-column prop="customerCategory" label="顾客类型" :formatter="(row) => $filter.dictConvert(row.customerCategory,'CustomerCategory')" :min-width="$helper.getColumnWidth(3)" show-overflow-tooltip>
        </el-table-column> -->
        <el-table-column prop="assistCollectorName" label="诉讼跟进人" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="currentCollectorName" label="电催/外访催收员" :min-width="$helper.getColumnWidth(5)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="flowInTime" label="案件创建时间" :min-width="$helper.getColumnWidth(4)" :formatter="(row) => $filter.dateTimeFormat(row.flowInTime)" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="lawerStatus" label="诉讼状态" :formatter="(row) => $filter.dictConvert(row.lawerStatus,'LawerStatus')" :min-width="$helper.getColumnWidth(4)" show-overflow-tooltip>
        </el-table-column>
      </template>
    </data-box>

    <!-- 重新分配 -->
    <el-dialog title="诉讼分案" :visible.sync="dialog.isOpenUserCaseAllot" width="800px" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false" @open="$nextTick(()=>$refs['user-case-allot'].refresh(lawDepartmentDataNoRoot,collectionType))">
      <user-case-allot :selectedCase="selectionList" @refreshList="refreshData" ref="user-case-allot" @close="dialog.isOpenUserCaseAllot=false;refreshData()"></user-case-allot>
    </el-dialog>

    <!-- 诉讼跟进-未确定诉讼类型的情况 -->
    <el-dialog title="诉讼跟进" center :visible.sync="dialog.isOpenLawsuitKind" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <lawsuit-kind ref="lawsuit-kind" @close="dialog.isOpenLawsuitKind = false;refreshData()" :rowData="rowData" :lawsuit="lawsuit" @refreshData="refreshData"></lawsuit-kind>
    </el-dialog>

    <!-- 诉讼跟进-已经确定诉讼类型的情况 -->
    <el-dialog title="诉讼跟进" center :visible.sync="dialog.isOpenLawsuitFollow" append-to-body width="900px" @open="() => this.$nextTick(() => this.$refs['lawsuit-follow'].refresh(rowData.lawerStatus))" @close="refreshData">
      <lawsuit-follow ref="lawsuit-follow" @close="dialog.isOpenLawsuitFollow = false;refreshData()" @refreshData="refreshData" :lawsuit.sync="lawsuit" :rowData="rowData" :isView="isView"></lawsuit-follow>
    </el-dialog>

    <!-- 费用管理 -->
    <el-dialog title="费用管理" :center="true" :visible.sync="dialog.isOpenCostMaintenance" width="1200px" @close="refreshData" @open="$nextTick(()=>$refs['cost-maintain'].refreshCostData())">
      <cost-maintain ref="cost-maintain" :rowData="rowData" @close="dialog.isOpenCostMaintenance = false;refreshData()" @refreshData="refreshData" ></cost-maintain>
    </el-dialog>

  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Auth, Layout, Dependencies } from '~/core/decorator';
import { PageService } from '~/utils/page.service';
import { SortService } from '~/utils/sort.service';
import DataForm from '~/components/common/data-form.vue';
import DataBox from '~/components/common/data-box.vue';
import UserCaseAllot from '~/components/distribution-common/user-case-allot.vue';
import NumberRange from '~/components/common/number-range.vue';
import { CaseAssitCaseService } from '~/services/domain-service/case-assist-case.service.ts';
import CostMaintain from "~/components/outsource-manage/outsource-lawsuit-manage/cost-maintain.vue";
import LawsuitKind from "~/components/lawsuit-follow/lawsuit-kind.vue";
import LawsuitFollow from "~/components/lawsuit-follow/lawsuit-follow.vue";
import { OutsourceLawsuitService } from "~/services/domain-service/outsource-lawsuit-manage-index.service";
import { OpenExecutionData } from "~/config/types";
import { ApplyType, CASE_CHANNEL, CASE_HALT_CATEGORY, } from '~/config/enum.config';
import { Mutation, Getter, namespace, State } from 'vuex-class';
const CollectionExecutionModule = namespace('collection-execution');


@Layout('workspace')
@Component({
  components: {
    DataForm,
    DataBox,
    UserCaseAllot,
    NumberRange,
    LawsuitKind,
    CostMaintain,
    LawsuitFollow,
  },
})
export default class AssistCase extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(CaseAssitCaseService)
  private caseAssitCaseService: CaseAssitCaseService;
  @Dependencies(OutsourceLawsuitService)
  private outsourceLawsuitService: OutsourceLawsuitService;
  @CollectionExecutionModule.Action private openCollectionExecution;

  @Getter
  private lawDepartmentDataNoRoot;
  @Getter
  private lawDepartmentData;
  private collectionType = 'LAW_CASE';

  private rowData: any = {};

  private get lawsuit() {
    return {
      kind: this.rowData.lawsuitFollowType,
      status: this.rowData.lawerStatus
    }
  }
  // 跟进是否只能查看
  private isView: boolean = false;

  private model: any = {
    areaIds: [],
    areaId: '',
    addressDetail: '',
    overdueDays: {
      max: '',
      min: '',
    },
    overdueAmount: {
      max: '',
      min: '',
    },
    turnSource: '',
    sysCode: '',
    creatTime: [],
    productName: '',
    lawNumber: '',
    customerCategory: '',
    overduePeriod: '',
    lawerStatus: '',
  };
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private dialog: any = {
    isOpenUserCaseAllot: false,
    isOpenLawsuitKind: false,
    isOpenLawsuitFollow: false,
    isOpenCostMaintenance: false,
  };

  private mounted() {
    this.refreshData();
  }

  private refreshData() {
    this.selectionList = []
    this.caseAssitCaseService.queryCaseAssistlist(this.model, this.pageService).subscribe(
      data => {
        this.dataSet = data
        if(this.dataSet){
          this.dataSet.forEach(x=>{
            x.overduePeriod>12?x.overduePeriod = 'M12+':x.overduePeriod = 'M'+x.overduePeriod
          })
        }
      }
    )
  }

  /**
   * 手动分案
   */
  private distributeClick() {
    if (this.selectionList.length === 0) {
      this.$message.info('请选择要分配的案件');
      return;
    }
    this.dialog.isOpenUserCaseAllot = true;
  }

  /**
   * 获取地区id
   */
  private getAreaId() {
    this.model.areaId =
      this.model.areaIds.length > 0
        ? this.model.areaIds[this.model.areaIds.length - 1]
        : '';
  }

  /**
   * 点击诉讼跟进
   */
  private lawsuitClick(rowData) {
    this.rowData = rowData
    // 如果存在诉讼类型 跳过选择诉讼类型的一步
    if (!rowData.lawsuitFollowType) {
      this.dialog.isOpenLawsuitKind=true
    } else {
      this.dialog.isOpenLawsuitFollow = true
    }
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    //const viewFlag = row.assistStatus === "ASSIST_OVER"
    this.openCollectionExecution(new OpenExecutionData(row.caseId, row.caseNumber, CASE_CHANNEL.assist, true,true))
  }

  /**
   * 范围输入框的重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: '', max: '' }
    this.model.overdueDays = { max: '', min: '' }
  }
}
</script>

<style lang="less">
</style>
